/*
 * FileSelection.java
 *
 * Created on December 10, 2006, 9:47 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package eu.somatik.somaftp.gui.dnd;

import eu.somatik.somaftp.logic.FileInterface;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.Arrays;

/**
 *
 * @author francisdb
 */
public class FileSelection implements Transferable{
    
    private static final String localFlavorType = DataFlavor.javaJVMLocalObjectMimeType +
                                ";class="+FileInterface.class.getName();
    
    // TODO http://java.sun.com/docs/books/tutorial/uiswing/dnd/examples/ArrayListTransferHandler.java
    // http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html
    
    public static final DataFlavor FLAVOR = new DataFlavor(FileInterface.class, "File transfer");
    
    private static final DataFlavor[] FLAVORS = new DataFlavor[]{FLAVOR, DataFlavor.stringFlavor};
    
    private FileInterface[] files;
    
    /** Creates a new instance of FileSelection */
    public FileSelection(FileInterface...files) {
        this.files = files;
    }

    public DataFlavor[] getTransferDataFlavors() {
        return FLAVORS;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return Arrays.binarySearch(FLAVORS, flavor) >= 0;
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if(flavor.equals(FLAVOR)){
            return files;
        }else if (flavor.equals(DataFlavor.stringFlavor)){
            StringBuilder builder = new StringBuilder();
            for(FileInterface file:files){
                if(builder.length() != 0){
                    builder.append(", ");
                }
                builder.append(file.getName());
            }
            return builder.toString();
        }else{
            return null;
        }
    }
    
}
